package servlet;

import com.alibaba.fastjson.JSON;
import entity.Admin;
import service.AdminService;
import service.impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AdminServlet", value = "/AdminServlet")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        AdminService adminService = new AdminServiceImpl();
        String opr = request.getParameter("opr");

        if(opr.equals("login")){
            String name = request.getParameter("username");
            String pwd = request.getParameter("password");
            if(adminService.verify(name,pwd)!=null){
                request.getSession().setAttribute("userName",name);
                request.getRequestDispatcher("/afterEnd/index.jsp").forward(request,response);
            }else {
                response.getWriter().print("<script>alert('账号或密码错误');location.href='afterEnd/login.jsp'</script>");
            }
        }

        if(opr.equals("adminList")){
            List<Admin> adminList = adminService.findAll();
            response.getWriter().print(JSON.toJSONString(adminList));
        }

        if(opr.equals("addAll")){
            Admin admin = new Admin();
            admin.setDiscount(request.getParameter("discount"));
            admin.setPassword(request.getParameter("pwd"));
            if(adminService.addAll(admin)>0){
                response.getWriter().print("<script>alert('添加成功');location.href='afterEnd/admin.jsp'</script>");
            }
        }

        if("update".equals(opr)){
            Admin admin = new Admin();
            admin.setDiscount(request.getParameter("discount"));
            admin.setPassword(request.getParameter("pwd"));
            admin.setId(Integer.parseInt(request.getParameter("id")));
            if(adminService.update(admin)>0){
                response.getWriter().print("<script>alert('修改成功');location.href='afterEnd/admin.jsp'</script>");
            }
        }

        if("findByid".equals(opr)){
            Admin admin = adminService.findId(Integer.parseInt(request.getParameter("id")));
            if(admin!=null){
                response.getWriter().print(JSON.toJSONString(admin));
            }
        }

        if("delid".equals(opr)){
            if(adminService.delById(Integer.parseInt(request.getParameter("id")))>0){
                response.getWriter().print("true");
            }else {
                response.getWriter().print("false");
            }
        }

        if("findAllByName".equals(opr)){
            List<Admin> findAllList = adminService.findAllByName(request.getParameter("name"));
            response.getWriter().print(JSON.toJSONString(findAllList));
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
